package com.buaa.aimall.goodsModule.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.buaa.aimall.Utils.BaseResponse;
import com.buaa.aimall.Utils.Enum.StatusCode;
import com.buaa.aimall.goodsModule.DTO.AddCollectDTO;
import com.buaa.aimall.goodsModule.Entity.CollectEntity;
import com.buaa.aimall.goodsModule.Service.CollectService;
import com.buaa.aimall.userModule.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods/collect")
@CrossOrigin
public class CollectController {
    @Autowired
    CollectService collectService;

    @Autowired
    UserService userService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addCollect(@RequestBody AddCollectDTO addCollectDTO) {
        if (addCollectDTO.getModel() == null && addCollectDTO.getCompute() == null) {
            return new BaseResponse<>(StatusCode.PARAM_ERROR, "模型和算力id不可同时为空", false);
        }
        if (addCollectDTO.getModel() != null && addCollectDTO.getCompute() != null) {
            return new BaseResponse<>(StatusCode.PARAM_ERROR, "模型和算力id不可同时不为空", false);
        }

        var user = userService.getCurrentUser();

        LambdaQueryWrapper<CollectEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollectEntity::getUser, user.getId());
        if (addCollectDTO.getModel() != null) {
            queryWrapper.eq(CollectEntity::getModel, addCollectDTO.getModel());
        } else {
            queryWrapper.eq(CollectEntity::getCompute, addCollectDTO.getCompute());
        }

        if (collectService.count(queryWrapper) > 0) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, "该商品已被收藏", false);
        }

        var collect = CollectEntity.builder()
                .user(user.getId())
                .model(addCollectDTO.getModel())
                .compute(addCollectDTO.getCompute())
                .build();
        var result = collectService.save(collect);
        return new BaseResponse<>(StatusCode.SUCCESS, "收藏成功", result);
    }


    @PostMapping("/delete")
    public BaseResponse<Boolean> delCollect(@RequestBody AddCollectDTO addCollectDTO) {
        if (addCollectDTO.getModel() == null && addCollectDTO.getCompute() == null) {
            return new BaseResponse<>(StatusCode.PARAM_ERROR, "模型和算力id不可同时为空", false);
        }
        if (addCollectDTO.getModel() != null && addCollectDTO.getCompute() != null) {
            return new BaseResponse<>(StatusCode.PARAM_ERROR, "模型和算力id不可同时不为空", false);
        }

        var user = userService.getCurrentUser();

        LambdaQueryWrapper<CollectEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollectEntity::getUser, user.getId());

        if (addCollectDTO.getModel() != null) {
            queryWrapper.eq(CollectEntity::getModel, addCollectDTO.getModel());
        } else {
            queryWrapper.eq(CollectEntity::getCompute, addCollectDTO.getCompute());
        }

        boolean result = collectService.remove(queryWrapper);

        if (!result) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, "取消收藏失败", false);
        }

        return new BaseResponse<>(StatusCode.SUCCESS, "取消收藏成功", true);
    }

}
